package com.hnevc.examliqiang;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;
import java.util.List;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class ShowActivity extends AppCompatActivity {

    private List<Food> foodList;

    private ListView mListView;
    private MyBaseAdapter adapter;
    private String url ="http://10.1.66.232:8080/RestaurantServer/Food/getFoods";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show);
        //TODO 功能实现题：获取数据并更新界面
        new Thread(new Runnable() {
            @Override
            public void run() {
                Request request = new Request.Builder()
                        .url(url)
                        .get()
                        .build();
                OkHttpClient client = new OkHttpClient();
                        Call call = client.newCall(request);
                try {
                    Response response = call.execute();
                    String data = response.body().string();
                    Gson gson = new Gson();
                    foodList = gson.fromJson(data, new TypeToken<List<Food>>(){}.getType());
                    runOnUiThread(new Runnable() {
                       @Override
                        public void run() {
                           mListView = (ListView) findViewById(R.id.id_lv_data);
                           adapter = new MyBaseAdapter(ShowActivity.this,foodList);
                           mListView.setAdapter(adapter);
                       }
                    });

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
       }).start();
    }
}
